package controllers

import (
	"finance/internal/dto"
	"finance/internal/message"
	"finance/internal/services"
	"finance/internal/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// GetAllTransactionsHandler @Summary 获取交易记录
// @Description 根据角色（customer/cashier/auditor）返回相应的交易记录
// @Tags Transaction
// @Accept json
// @Produce json
// @Param account_no query string false "账户号，仅cashier可指定"
// @Param page query int false "页码，默认1"
// @Param page_size query int false "每页数量，默认10"
// @Success 200 {object} utils.Response{data=[]models.Transaction} "成功返回交易记录列表"
// @Failure 400 {object} utils.Response "参数错误"
// @Failure 403 {object} utils.Response "无权限"
// @Router /transaction/getRecords [get]
func GetAllTransactionsHandler(c *gin.Context) {
	role, _ := c.Get("role")
	userID := c.GetUint("userID")

	accountNo := c.Query("account_no")
	pageStr := c.DefaultQuery("page", "1")
	pageSizeStr := c.DefaultQuery("page_size", "10")

	page, _ := strconv.Atoi(pageStr)
	pageSize, _ := strconv.Atoi(pageSizeStr)
	offset := (page - 1) * pageSize

	transactions, total, err := services.GetTransactions(role.(string), userID, accountNo, offset, pageSize)
	if err != nil {
		utils.Fail(c, 400, err.Error())
		return
	}

	utils.Success(c, gin.H{
		"list":      transactions,
		"total":     total,
		"page":      page,
		"page_size": pageSize,
	})
}

// DepositHandler @Summary Account Deposit
// @Description Account Deposit
// @Tags Transaction
// @Accept json
// @Produce json
// @Param deposit body dto.DepositRequest true "Deposit info"
// @Success 200 {object} dto.DepositResponse
// @Failure 400 {object} utils.Response
// @Router /transaction/deposit [POST]
func DepositHandler(c *gin.Context) {
	role, _ := c.Get("role")      // 从Token解析出来的role
	userID := c.GetUint("userID") // 从Token解析出来的真实ID

	if role != "customer" && role != "cashier" {
		utils.Fail(c, http.StatusForbidden, message.PermissionDenied)
	}

	var req dto.DepositRequest

	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Fail(c, 400, "invalid request")
		return
	}

	err := services.Deposit(&req, userID)
	if err != nil {
		utils.Fail(c, http.StatusBadRequest, err.Error())
		return
	}

	utils.Success(c, message.DepositSuccess)
}

// WithdrawHandler DepositHandler @Summary Account Deposit
// @Description Account Deposit
// @Tags Transaction
// @Accept json
// @Produce json
// @Param withdrawal body dto.WithdrawRequest true "Withdrawal info"
// @Success 200 {object} utils.Response // [TODO]
// @Failure 400 {object} utils.Response
// @Router /transaction/withdraw [POST]
func WithdrawHandler(c *gin.Context) {
	role, _ := c.Get("role")
	userID := c.GetUint("userID")

	if role != "customer" && role != "cashier" {
		utils.Fail(c, http.StatusForbidden, "Access denied")
		return
	}

	var req struct {
		AccountID uint    `json:"account_id"`
		Amount    float64 `json:"amount"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Fail(c, 400, "invalid request")
		return
	}

	isCashier := role == "cashier"
	err := services.Withdraw(req.AccountID, req.Amount, userID, isCashier)
	if err != nil {
		utils.Fail(c, 400, err.Error())
		return
	}

	utils.Success(c, message.WithdrawSuccess)
}

// TransferHandler @Summary
// @Description
// @Tags Transaction
// @Accept json
// @Produce json
// @Param transfer body dto.TransferRequest true "transfer info"
// @Success 200 {object} utils.Response
// @Failure 400 {object} utils.Response
// @Router /transaction/transfer [POST]
func TransferHandler(c *gin.Context) {
}
